package com.itheima.bos.web.action;

import com.itheima.bos.domain.Workordermanage;
import com.itheima.bos.service.IWorkordermanageService;
import com.itheima.bos.utils.BOSContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr.King on 2017/3/21.
 */
@Controller
@Scope("prototype")
public class TaskAction extends ActionSupport{


    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private IWorkordermanageService workordermanageService;

    /**
     * 查询组任务
     * @return
     */
    public String findGroupTask(){
        TaskQuery query = processEngine.getTaskService().createTaskQuery();
        String id= BOSContext.getLoginUser().getId();
        query.taskCandidateUser(id);
        List<Task> list = query.list();
        ActionContext.getContext().getValueStack().set("list",list);
        return "groupList";
    }

    //任务id
    private String taskId;
    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }
    public String getTaskId() {
        return taskId;
    }

    /**
     * 查询业务数据
     */
    public String showData() throws IOException {
        Map<String, Object> variables = processEngine.getTaskService().getVariables(taskId);
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        ServletActionContext.getResponse().getWriter().print(variables);
        return NONE;
    }

    /**
     * 拾取组任务
     */
    public String takeTask(){
        String id= BOSContext.getLoginUser().getId();
        processEngine.getTaskService().claim(taskId,id);
        return "toGroupList";
    }

    /**
     * 查询个人任务
     */
    public String findPersonalTask(){
        String uid = BOSContext.getLoginUser().getId();
        TaskQuery query = processEngine.getTaskService().createTaskQuery();
        List<Task> list = query.taskAssignee(uid).list();
        ActionContext.getContext().getValueStack().set("list",list);
        return "personalList";
    }

    //是否审核通过
    private String check;
    public void setCheck(String check) {
        this.check = check;
    }

    /**
     * 办理个人任务
     */
    public String checkWorkOrderManage(){
        Map<String, Object> map = processEngine.getTaskService().getVariables(taskId);
        if (check == null){
            //跳转到审核工作单
            ActionContext.getContext().getValueStack().set("map",map);
            return "toCheck";
        }else {
            //将managerCheck改为1
            Workordermanage workordermanage = (Workordermanage) map.get("业务数据");
            workordermanage.setManagerCheck("1");
            workordermanageService.update(workordermanage);

            Map<String, Object> variables = new HashMap<>();
            variables.put("业务数据",workordermanage);
            variables.put("check",check);
            processEngine.getTaskService().complete(taskId,variables);

            //办理任务
            if (check.equals("0")){
                //审核不通过
                workordermanage.setStart("0");
                workordermanageService.update(workordermanage);
            }
        }
        return "toPersonalList";
    }

    /**
     * 办理其他任务
     */
    public String outStore(){
        processEngine.getTaskService().complete(taskId);
        return "toPersonalList";
    }

    public String transferGoods(){
        processEngine.getTaskService().complete(taskId);
        return "toPersonalList";
    }

    public String receive(){
        processEngine.getTaskService().complete(taskId);
        return "toPersonalList";
    }

}
